import Vue from 'vue'

Vue.prototype.setCookie = function (key, value, expireDays) {
    var exDate = new Date()
    exDate.setDate(exDate.getDate() + expireDays)
    document.cookie = key + "=" + escape(value) +
        ((expireDays == null) ? "" : ";expires=" + exDate.toGMTString())
};


//获取cookie

Vue.prototype.getCookie = function (c_name) {
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            var c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
};

Vue.prototype.clearCookie = function (name) {
    this.setCookie(name, "", -1);
}

Vue.prototype.clearAllCookie = function () {
    if (document.cookie.length == -1) return;
    document.cookie.split(';').forEach(str => {
        var i = str.indexOf('=');
        var name = str.substring(0, i).trim();
        if (name != '') {
            this.setCookie(name, "", -1);
        }
    })
}